const ajax = require("../../../utils/ajax/ajax")
const util = require("../../../utils/util")

Page({
    data: {
        isDisabled: true,
        formData: {
            userName: '',
            userCard: '',
            pickerIndex: '',
            recommendCode: ''
        },
        pickerList: ['黑金', '铂金', '黄金'],
        isPicker: true
    },
    // input 焦点事件
    inputChange(e) {
        const dataset = e.currentTarget.dataset
        const value = e.detail.value
        const formData = this.data.formData
        formData[dataset.label] = value
        this.setData({
            formData,
            isDisabled: false
        })
    },
    // 显示 Picker 选择器
    showPicker() {
        this.setData({
            isPicker: false
        })
    },
    // Picker 选择器 取消
    pickerCancel() {
        this.setData({
            isPicker: true
        })
    },
    // Picker 选择器 确定
    pickerConfirm(e) {
        const index = e.detail.index
        const formData = this.data.formData
        formData.pickerIndex = index
        this.setData({
            formData,
            isPicker: true
        })
    },
    // 提交审核
    toApplySuccess() {
        const {
            userName,
            userCard,
            pickerIndex,
            recommendCode
        } = this.data.formData
        if (!userName) {
            util.toast('请输入姓名')
            return
        }
        const regCard = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        if (!regCard.test(userCard)) {
            util.toast('身份证号有误')
            return
        }
        if (pickerIndex === '') {
            util.toast('请选择会员等级')
            return
        }
        ajax.post({
            url: '/wx/submitMemberVerify',
            data: {
                user_name: userName,
                id_card: userCard,
                grade: pickerIndex + 1,
                be_rec_code: recommendCode || ''
            },
            success() {
                wx.navigateTo({
                    url: '/pages/Index/applySuccess/applySuccess',
                })
            }, 
            fail(err) {
                util.toast(err.msg)
            }
        })
    }
})